﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;

    public class KRC extends MovieClip {

        public var api:Object
        public var krc:String
        public var miarry:Array
        private var list:Array
        private var split:String = "{|}"
        private var reg_tabo:RegExp
        private var reg_line:RegExp
        private var reg_word:RegExp

		private var loader:URLLoader;
		
        public function KRC():void{
            this.miarry = [64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105];
            this.reg_tabo = /\[(ti|ar|al|by|offset|total):(.+)\]\s+/ig;
            this.reg_line = /(\[(\d+),(\d+)\])(.+)\s+/ig;
            this.reg_word = /<(\d+),(\d+),(\d+)>([^<]*)/ig;
            super();
            Security.allowDomain("*");
			
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.load(new URLRequest("assets/happy.krc"));
			
//            root.loaderInfo.sharedEvents.addEventListener("api", this.apiHandler);
//            root.loaderInfo.sharedEvents.addEventListener("api_remove", this.removeHandler);
        }
		
	
		
        public function removeHandler(_arg1):void{
        }
		
        private function apiHandler(_arg1):void{
            var _local2:Object = _arg1.data;
            if (!_local2){
                return;
            };
            this.api = _local2.api;
        }
        public function loadHandler(_arg1:Object):void{
            this.krc = null;
        }
		
		protected function onComplete(event:Event):void
		{
			var data:ByteArray = URLLoader(event.target).data as ByteArray;
			var l:* = 0;
			var ba:* = data;
			var len:int = ba.length;
			var head:ByteArray = new ByteArray();
			head.writeBytes(ba, 0, 3);
			var tag:String = head.toString();
			if (tag != "krc"){
				return;
			}
			var zip_byte:ByteArray = new ByteArray();
			zip_byte.writeBytes(ba, 4);
			zip_byte.position = 0;
			var j:int = zip_byte.length;
			var k:int = 0;
			while (k < j) {
				l = (k % 16);
				zip_byte[k] = int((zip_byte[k] ^ this.miarry[l]));
				k = (k + 1);
			};
			try {
				zip_byte.uncompress();
			} catch(e:Error) {
				trace(e);
				return;
			};
			event.stopImmediatePropagation();
			this.krc = zip_byte.toString();
			this.parseKrc();
		}
		
       
        public function parseKrc():void{
            var _local4:String;
            var _local5:XML;
            var _local7:String;
            var _local8:int;
            var _local9:Object;
            var _local10:String;
            var _local11:XML;
            var _local1:String = this.krc;
            _local1 = (_local1 + "\n");
            var _local2:Array = _local1.match(this.reg_tabo);
            var _local3:Array = [];
            for each (_local4 in _local2) {
                _local7 = _local4.replace(this.reg_tabo, this.setTabo);
                if (_local7){
                    _local3.push(_local7);
                };
            };
            _local5 = new XML((("<kmc " + _local3.join(" ")) + "></kmc>"));
            this.list = [];
            _local1.replace(this.reg_line, this.setLine);
            if (this.list.length){
                _local8 = 0;
                while (_local8 < this.list.length) {
                    _local9 = this.list[_local8];
                    _local10 = (((((((("<l t=\"" + _local9.start) + ",") + _local9.end) + ",") + _local9.time) + "\">") + _local9.text) + "</l>");
                    _local11 = new XML(_local10);
                    _local5.appendChild(_local11);
                    _local8++;
                };
            };
            var _local6:String = _local5.toXMLString();
			trace(_local6);
        }
        private function setTabo():String{
            var _local2:String = arguments[1];
            var _local3:String = arguments[2];
            if (_local2 == "total"){
                _local2 = "duration";
                _local3 = this.formatTime(parseInt(_local3));
            } else {
                _local3 = _local3.replace(/</g, "&lt;");
                _local3 = _local3.replace(/>/g, "&gt;");
                _local3 = _local3.replace(/"/g, "&quot;");
                _local3 = _local3.replace(/@/g, "&amp;");
            };
            return ((((_local2 + "=\"") + _local3) + "\""));
        }
        private function setLine():String{
            var _local2:Number = parseInt(arguments[2]);
            var _local3:Number = parseInt(arguments[3]);
            var _local4:Number = (_local2 + _local3);
            var _local5:String = arguments[4];
            var _local6:String = _local5.replace(this.reg_word, this.parseTail);
            var _local7:Array = _local6.split(this.split);
            var _local8 = "";
            var _local9:Array = [];
            var _local10:int;
            while (_local10 < (_local7.length - 1)) {
                _local9.push(_local7[_local10]);
                _local8 = (_local8 + _local7[(_local10 + 1)]);
                _local10 = (_local10 + 2);
            };
            this.list.push({start:this.formatTime(_local2, true), end:this.formatTime(_local4, true), text:_local8, time:_local9.join(",")});
            return ("");
        }
        private function parseTail():String{
            var _local2:int = parseInt(arguments[2]);
            var _local3:String = arguments[4];
            if (_local3.length > 1){
                _local3 = (("[" + _local3) + "]");
            };
            return ((((_local2 + this.split) + _local3) + this.split));
        }
        private function getDuration(_arg1:String):int{
            var _local5:Number;
            var _local2:Array = _arg1.split(/\s*\:\s*/);
            var _local3:int = _local2.length;
            var _local4:Number = 0;
            while (_local2.length) {
                _local5 = parseFloat(_local2.pop());
                if (!isNaN(_local5)){
                    _local4 = (_local4 + (_local5 * Math.pow(60, ((_local3 - _local2.length) - 1))));
                };
            };
            _local4 = int((_local4 * 1000));
            return (_local4);
        }
        private function formatTime(_arg1:Number, _arg2:Boolean=false):String{
            var _local6:String;
            var _local3 = "";
            var _local4:Number = (_arg1 / 1000);
            var _local5:int = int(_local4);
            _local3 = ((this.zero((_local5 / 60)) + ":") + this.zero((_local5 % 60)));
            if (_arg2){
                _local6 = _local4.toString();
                _local3 = (_local3 + _local6.substr(_local6.indexOf(".")));
            };
            return (_local3);
        }
        public function zero(_arg1:Number):String{
            var _local2:String = String(int(_arg1));
            if (_local2.length < 2){
                _local2 = ("0" + _local2);
            };
            return (_local2);
        }

    }
}//package 
